package com.shanghaionstar.carmanage.presenter;

import com.onstar.cn.common.exception.OnStarException;
import com.onstar.cn.framework.mvp.base.IMVPScheduler;
import com.onstar.cn.framework.mvp.base.IMVPView;
import com.onstar.cn.framework.mvp.base.core.BasePresenter;
import com.onstar.cn.smart.entity.PayChannelResponseEntity;
import com.shanghaionstar.carmanage.iview.IPayChannelView;
import com.shanghaionstar.carmanage.service.PaymentChannelService;

import rx.functions.Action1;

/**
 * Created by 6335yz on 2016/7/24.
 */
public class PayChannelPresenter extends BasePresenter {
    private IPayChannelView iPayChannelView;
    private PaymentChannelService service;

    public PayChannelPresenter(IMVPScheduler mvpScheduler) {
        super(mvpScheduler);
    }

    @Override
    public void setView(IMVPView view) {
        super.setView(view);
        iPayChannelView = (IPayChannelView) view;
        service = new PaymentChannelService(iPayChannelView.onActivity());
        getPayChannelData();
    }

    public void getPayChannelData() {
        iPayChannelView.setLoading(true);
        subscription = service.paymentChannelList("1", "10")
                .subscribeOn(mvpScheduler.backgroundThread())
                .observeOn(mvpScheduler.mainThread())
                .subscribe(new Action1<PayChannelResponseEntity>() {
                    @Override
                    public void call(PayChannelResponseEntity responseEntity) {
                        if (iPayChannelView != null) {
                            iPayChannelView.setLoading(false);
                            iPayChannelView.onReponseSuccess(responseEntity);
                        }
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        //里面抛出的Error信息
                        if (iPayChannelView != null) {
                            iPayChannelView.setLoading(false);
                            iPayChannelView.onResponseError((OnStarException) throwable);
                        }
                    }
                });
    }
}
